# coding:utf-8
# utils/id_mapping.py
import pandas as pd


def map_to_entrez(protein_ids, mapping_file="data/mapping/uniprot_entrez.csv"):
    """将 UniProt ID 映射为 Entrez Gene ID"""
    mapping_df = pd.read_csv(mapping_file)
    return [mapping_df[mapping_df["uniprot"] == pid]["entrez"].values[0] for pid in protein_ids]


def read_ppi_data(ppi_file):
    """读取 PPI 数据并返回 (source, target, confidence_score)"""
    df = pd.read_csv(ppi_file, sep='\t')
    df['source'] = map_to_entrez(df['source'])
    df['target'] = map_to_entrez(df['target'])
    return df[['source', 'target', 'confidence']].values.tolist()


def read_expression_data(expr_file):
    """读取基因表达数据，计算皮尔逊相关性"""
    import numpy as np
    expr_df = pd.read_csv(expr_file, index_col=0)
    corr_matrix = np.corrcoef(expr_df.values)
    edges = []
    genes = expr_df.index.tolist()
    for i in range(len(genes)):
        for j in range(i + 1, len(genes)):
            if abs(corr_matrix[i][j]) > 0.5:
                edges.append((genes[i], genes[j], abs(corr_matrix[i][j])))
    return edges
